<?php
/* 
 * @Package Softpin Order Manager
 * @Subpackage : sim Manager
 * Create by : thuc.lehuy@gmail.com
 */

// no direct access
defined('_JEXEC') or die('Truy nhập không hợp lệ');
// import model parent
jimport( 'joomla.application.component.model' );

/*
 *  sim Model
 * Get data from table sim
 */
class SimsModelSims extends JModel
{
    var $_data = null;
	var $_total = null;
	var $_pagination = null;
	var $_limit = 15;
	var $_limitstart = 0;
	 
	 
	 public function __construct(){
		
		parent::__construct();
		
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		
		$this->_limit = $mainframe->getUserStateFromRequest( $context.$view.'limit', 'limit', $mainframe->getCfg('list_limit'), 20);
		$limitstart = JRequest::getInt('limitstart');
		//var_dump($limitstart);
		$this->_limitstart = empty($limitstart) ? 0 : $limitstart;
		
		$this->setState('limit', $this->_limit);
		$this->setState('limitstart', $this->_limitstart);
	}
	
	public function getTotal(){
		
		if (empty($this->_total)){
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}
		return $this->_total;
	}

	public function getPagination(){
	
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->_limitstart, $this->_limit );
		}
		return $this->_pagination;
	}
	
	
	 /**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
    function _buildQuery()
    {
        $query = 'select S.*,SA.admin_name,SP.provider_name,SC.categori_name from #__sim S,#__sim_admin SA,#__sim_categories SC,#__sim_provider SP where S.admin_id=SA.admin_id and S.provider_id=SP.provider_id and S.categori_id=SC.categori_id order by del_flag,sim_id';
        return $query;
    }
    // Get sim in DB
    function getData()
    {
        $query = $this->_buildQuery();
		//echo $query;
		if (empty($this->_data)){
		
			$this->_db->setQuery( $query, $this->_limitstart, $this->_limit );
			$this->_data = $this->_db->loadObjectList();
		}
		
		$data = $this->_data;
		return $data;
    }    
}